package com.atguigu.reggie.service.impl;

import com.atguigu.reggie.dto.DishDto;
import com.atguigu.reggie.entity.Dish;
import com.atguigu.reggie.entity.DishFlavor;
import com.atguigu.reggie.entity.SetmealDish;
import com.atguigu.reggie.mapper.DishMapper;
import com.atguigu.reggie.selfException.CustomException;
import com.atguigu.reggie.service.DishFlavorService;
import com.atguigu.reggie.service.DishService;
import com.atguigu.reggie.service.SetmealDishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Luo Sheng
 * @school YiBin university
 * @create 2022-10-12 15:37
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        // 保存菜品的基本信息到菜品表dish,save方法只在乎传入对象是否包含对应的参数
        this.save(dishDto);

        if(dishDto.getFlavors() == null){
            log.info("flavor=null");
            return;
        }

        // 获取dishId
        Long dishId = dishDto.getId();

        // 循环遍历菜品口味并给dishId赋值
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }

        // 保存菜品口味数据到菜品口味表dish_flavor  saveBatch():批量保存
        dishFlavorService.saveBatch(flavors);
    }

    /**
     *  根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        // 1、查询菜品基本信息
        Dish dish = this.getById(id);

        // 2、对象拷贝
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        // 3、查询菜品口味信息,从dish_flavor中查询
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);

        // 给dishDto对象flavors属性赋值
        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 根据菜品信息更新菜品
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        // 更新dish表
        this.updateById(dishDto);

        // 删除dish_flavor表原有数据
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

        log.info("flavor为：{}",dishDto.getFlavors());

        // 再添加现有flavor数据
        List<DishFlavor> dishFlavors = dishDto.getFlavors();
        dishFlavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    /**
     * 根据id删除菜品信息
     * @param ids
     */
    @Override
    public void removeWithFlavorByIds(List<Long> ids) {
        // 确定要删除的dish没在套餐中
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        int count1 = setmealDishService.count(setmealDishLambdaQueryWrapper);
        if (count1 >= 1){
            throw new CustomException("菜品在套餐中，不能删除");
        }

        // 确定dish状态为停售
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        int count2 = this.count(dishLambdaQueryWrapper);
        if (count2 >= 1){
            throw new CustomException("菜品正在售卖中，不能删除");
        }
        // 先删除对应的DishFlavor
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
            dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

        // 再删除菜品信息
        this.removeByIds(ids);
    }
}
